package com.xuzhijie.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Feedback;
import com.xuzhijie.pms.entity.Owner;
import com.xuzhijie.pms.entity.Property;
import com.xuzhijie.pms.entity.vo.PropertyVo;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.mapper.OwnerMapper;
import com.xuzhijie.pms.mapper.PropertyMapper;
import com.xuzhijie.pms.service.IOwnerService;
import com.xuzhijie.pms.service.IPropertyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-16
 */
@Service
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements IPropertyService {

    @Resource
    public OwnerMapper ownerMapper;

    @Override
    public Result findPage(Integer page,
                           Integer size,
                           Integer type,
                           Integer checkIn,
                           String number) {
        Page<Property> PropertyPage = new Page<>(page,size);
        QueryWrapper<Property> queryWrapper = new QueryWrapper<>();
        Page<Property> result;
        Long total;

        if (!type.equals(0)){
            queryWrapper.eq("type",type);
        }

        if (!checkIn.equals(0)){
            queryWrapper.eq("check_in",checkIn);
        }

        if (StrUtil.isNotBlank(number)){
            queryWrapper.like("number",number);
        }

        try {
            result = page(PropertyPage,queryWrapper);
            total = count(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            throw new PmsServerException("业务繁忙", Constants.SYSTEM_ERROR);
        }

        List<Property> propertyList = result.getRecords();
        List<PropertyVo> voList = new ArrayList<>();
        for (Property property : propertyList) {
            PropertyVo propertyVo = new PropertyVo(property);
            BeanUtil.copyProperties(property,propertyVo);
            voList.add(propertyVo);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("records",voList);
        resultMap.put("total",total);

        System.out.println(propertyList);

        return Result.success(resultMap);
    }

    @Override
    public Result getOwnerAndProperty(String phone,Integer page,Integer size,String number) {
        if (StrUtil.isBlank(phone)){
            return Result.error();
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<Owner> ownerQueryWrapper = new QueryWrapper<>();
        ownerQueryWrapper.eq("phone",phone);
        Owner owner = ownerMapper.selectOne(ownerQueryWrapper);

        if (BeanUtil.isEmpty(owner)){
            return Result.error(Constants.ILLEGAL_PARAMETERS,"业主未创建");
        }

        Page<Property> pages = new Page<>(page,size);
        QueryWrapper<Property> propertyQueryWrapper = new QueryWrapper<>();
        propertyQueryWrapper.eq("check_in",2);

        if (StrUtil.isNotBlank(number)){
            propertyQueryWrapper.eq("number",number);
        }

        Page<Property> propertyPage = page(pages, propertyQueryWrapper);
        List<Property> list = propertyPage.getRecords();
        List<PropertyVo> voList = new ArrayList<>();
        for (Property property : list) {
            PropertyVo propertyVo = new PropertyVo(property);
            BeanUtil.copyProperties(property,propertyVo);
            voList.add(propertyVo);
        }

        long count = count(propertyQueryWrapper);

        //获取该业主已绑定的房产
        QueryWrapper<Property> propertyQueryWrapper1 = new QueryWrapper<>();
        propertyQueryWrapper1.eq("owner",owner.getId());
        List<Property> properties = list(propertyQueryWrapper1);
//        List<Property> properties = list(lambdaQuery().eq(Property::getOwner, owner.getId()));
        if (ArrayUtil.isEmpty(properties)){
            resultMap.put("propertyDetails","暂无");
        }
        List<String> propertyDetails = properties.stream().map(Property::getDetail).collect(Collectors.toList());

        resultMap.put("ownerInfo",owner);
        resultMap.put("propertyList",voList);
        resultMap.put("total",count);
        resultMap.put("propertyDetails",propertyDetails);
        return Result.success(resultMap);
    }

    @Override
    public Result bindingPropertyWithOwner(String ownerid, String propertyId) {

        if (StrUtil.isBlank(ownerid) || StrUtil.isBlank(propertyId))
        {
            return Result.error();
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = now.format(dateTimeFormatter);
        Property property = getById(propertyId);
        property.setOwner(ownerid);
        property.setCheckIn(1);
        property.setCheckInTime(LocalDate.parse(format));

        saveOrUpdate(property);

        return Result.success();
    }

    @Override
    public Result WxGetPropertyWithOwner(String id) {
        QueryWrapper<Property> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner",id);
        List<Property> list = list(queryWrapper);
        List<PropertyVo> voList = new ArrayList<>();
        for (Property property : list) {
            PropertyVo propertyVo = new PropertyVo(property);
            BeanUtil.copyProperties(property,propertyVo);
            voList.add(propertyVo);
        }

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("propertyList",voList);
        resultMap.put("total",voList.size());

        return Result.success(resultMap);
    }

    @Override
    public Result WxGetPropertyWithOwnerWhereReno(String id, Integer renovationStatus) {
        QueryWrapper<Property> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner",id);
        queryWrapper.eq("renovation_status",renovationStatus);
        List<Property> list = list(queryWrapper);
        List<PropertyVo> voList = new ArrayList<>();
        for (Property property : list) {
            PropertyVo propertyVo = new PropertyVo(property);
            BeanUtil.copyProperties(property,propertyVo);
            voList.add(propertyVo);
        }

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("propertyList",voList);
        resultMap.put("total",voList.size());

        return Result.success(resultMap);
    }
}
